package server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * represents a single measurement at a specific Referencepoint.
 * tablename is the name of the table the samples are stored in.
 * loadSamplesFromDB() loads the samples from the Database to "samples".
 * calculateMeanValues() calculates the Meanvalues and stores them into
 * "meanvalues".
 * @author nemi
 *
 */
public class SinglePoint {
	public SinglePoint(String tablename) {
		super();
		this.tablename = tablename;
		this.samples=new ArrayList<DataSet>();
		this.meanvalues=new HashMap<String,MeanValue>();
	}

	public String tablename;
	public ArrayList<DataSet> samples;
	public Map<String,MeanValue> meanvalues; //mapping von SpotID zu MeanValues
	
	
	public void calculateStandardDeviation(){
		double var=0;
		for(String spot: meanvalues.keySet()){
			
			for(int i=0;i<samples.size();i++){
				var=var+(samples.get(i).getRssi()-meanvalues.get(spot).getMeanrssi())*(samples.get(i).getRssi()-meanvalues.get(spot).getMeanrssi());
			}
			meanvalues.get(spot).setSdrssi(Math.sqrt(var));
		}
	}
	
	/**
	 * calculates the meanvalues of RSSI,LQI and PLR
	 */
	public boolean calculateMeanValues(){
		
		if(samples.isEmpty()==false){
			
			//needed for the packetlossrate (spot to sequence number)
			Map<String,Integer> lastSeqNrs=new HashMap<String,Integer>();
			Map<String,Integer> firstSeqNrs=new HashMap<String,Integer>();
			
			//counter in a packet (spot to seen samples)
			Map<String,Integer> packetcount=new HashMap<String,Integer>(); 
			
			meanvalues=new HashMap<String,MeanValue>();
			
			//number of packets in a table
			int packetcounter=0; 
			
			DataSet currentDataSet=null;
			
			samples.get(0).setNewpacket(false); //set first to false, because we have nothing to calculate (plr) at this point
			
			int index=0;
			//go through the samples
			for(int k=0;k<samples.size();k++){
				
				currentDataSet=samples.get(k); //pick one
				String spotID=currentDataSet.getSpotId();
				
				//if we have no meanvalues of this Spot we add him
				if(!meanvalues.containsKey(spotID)){
					meanvalues.put(spotID, new MeanValue(0, 0, 0,0,0));
					
					packetcount.put(spotID,0); //init the packetcounter
					
					lastSeqNrs.put(spotID,currentDataSet.getSeqNr());
					firstSeqNrs.put(spotID,currentDataSet.getSeqNr());
					
				}
								
				//for RSSI an LQI we simply add them up. Later we will divide by n
				meanvalues.get(spotID).setMeanrssi(meanvalues.get(spotID).getMeanrssi()+currentDataSet.getRssi());
				meanvalues.get(spotID).setMeanlqi(meanvalues.get(spotID).getMeanlqi()+currentDataSet.getLQI());
				
				
				//we come to a new packet and calculate the packetlossrate
				if(currentDataSet.isNewpacket()){
					//do this for every fixspot we have meanvalues for
					for(String key:meanvalues.keySet()){
						//we need at least two packets to calculate a lossrate
						if(firstSeqNrs.get(key)!=lastSeqNrs.get(key)){
							float newrate=(lastSeqNrs.get(key)-firstSeqNrs.get(key)+1)/packetcount.get(key); //the new partial lossrate
							//add up the partial lossrates, later we will divide by the packetcount
							meanvalues.get(key).setPacketLossRate(meanvalues.get(key).getPacketLossRate()+newrate);
						}
						
						//invalid the sequence numbers and reset the inter-packet counter
						firstSeqNrs.put(key,-1);
						lastSeqNrs.put(key, -1);
						packetcount.put(key, 0);
					}
					packetcounter++;
				}
				
				//revalid the first sequence number of a fixed spot
				if(!firstSeqNrs.isEmpty() && firstSeqNrs.get(spotID)==-1){
					firstSeqNrs.put(spotID,currentDataSet.getSeqNr());
				}
				
				//update the last sequence number of a fixed spot
				lastSeqNrs.put(spotID,currentDataSet.getSeqNr());
				
				//increase the "n-counter" for the meanvalues
				meanvalues.get(spotID).setCount(meanvalues.get(spotID).getCount()+1);
				
				//increase the inter-packet count
				packetcount.put(spotID,packetcount.get(spotID)+1);
			}
						
			//in the end: go through all mean values (the different spots)
			for(String key:meanvalues.keySet()){
				//divide by n
				meanvalues.get(key).setMeanrssi(meanvalues.get(key).getMeanrssi()/meanvalues.get(key).getCount());
				meanvalues.get(key).setMeanlqi(meanvalues.get(key).getMeanlqi()/meanvalues.get(key).getCount());
				
				
				//calc the last partial packet loss rates for every fixed spot
				if(firstSeqNrs.get(key)!=lastSeqNrs.get(key)){
					float newrate=(lastSeqNrs.get(key)-firstSeqNrs.get(key)+1)/packetcount.get(key); //new rate berechnen
					meanvalues.get(key).setPacketLossRate(meanvalues.get(key).getPacketLossRate()+newrate);
					packetcounter++;
				}
				
				//calc the mean packetlossrate from the sum of the partial packetlossrates
				meanvalues.get(key).setPacketLossRate(meanvalues.get(key).getPacketLossRate()/packetcounter);
			}
			//we successfully calculated the meanvalues
			return true;				
		}
		
		//samples were empty
		return false;
		
	}
}

